<?php
class visitzyController extends AController {
	
	// 住院回访
	public function indexAction() {
		$page = $this->request->getGetParameter('p', 1);
		$pageSize = 10;
	
		// 搜索条件记住，在重新搜索后才有用
		if (isset($_GET['search'])) {
			$sel = array();
		} else {
			$sel = $_SESSION['zy_select_params'];
		}
		$startTime = $sel['starttime'] = $this->request->getGetParameter('startTime',$sel['starttime']);
		$endTime = $sel['endtime'] = $this->request->getGetParameter('endTime',$sel['endtime']);
		$selDep = $sel['selDep'] = $this->request->getGetParameter('selDep',$sel['selDep']);
		$selStatus = $sel['selStatus'] = $this->request->getGetParameter('selStatus',$sel['selStatus']);
		$keyword = $sel['keyword'] = $this->request->getGetParameter('keyword',$sel['keyword']);
		$_SESSION['zy_select_params'] = $sel;
		$this->selDep = $selDep;
		ATools::addSquote($selDep);
		ATools::addSquote($selStatus);

		$where = '1=1'.($startTime ? " AND ZY.CY_DATE>='{$startTime}'" : '')
				.($endTime ? " AND ZY.CY_DATE<='{$endTime}'" : '')
				.($keyword ? " AND (P.TRUE_NAME LIKE '%{$keyword}%' OR ZY.ZYDOC LIKE '%{$keyword}%')" : '')
				.(count($selDep) > 0 ? " AND ZY.CY_DEP_NO IN (".implode(',', $selDep).")" : '')
				.(count($selStatus) > 0 ? 
					(in_array("'A'", $selStatus)?  " AND (LV.VISIT_STATE IN (".implode(',', $selStatus).") OR 
							LV.VISIT_STATE IS NULL)" : (" AND LV.VISIT_STATE IN (".implode(',', $selStatus).")")) : '');
		
		$fields = "P.PATIENT_ID, P.TRUE_NAME, P.PHONE, P.SEX, P.BIRTH, ZY.RY_DATE, ZY.CY_DATE, ZY.CY_DEP, ZY.ZYDOC, ZY.CY_DEP_NO, ZY.ZYDOC_NO, ZY_AGE, ZY.BIZ_CODE,
		LV.LAST_VISIT_TIME, LV.VISIT_COUNT, LV.VISIT_RESULT, LV.VISIT_STATE";
		
		$sql = "SELECT {$fields} FROM ZHUYUAN ZY 
				LEFT JOIN PATIENT P ON ZY.REF_PATIENT_ID=P.PATIENT_ID
				LEFT JOIN LAST_VISIT LV ON 'ZY_'||ZY.ZY_NO||'_'||ZY.ZY_TIMES=LV.BIZ_CODE
				WHERE {$where}
				GROUP BY P.PATIENT_ID, P.TRUE_NAME, P.PHONE, P.BIRTH, P.SEX, ZY.RY_DATE, ZY.CY_DATE, ZY.CY_DEP, ZY.ZYDOC, ZY_AGE, ZY.BIZ_CODE, ZY.ZY_CODE, ZY.CY_DEP_NO, ZY.ZYDOC_NO,
				LV.LAST_VISIT_TIME, LV.VISIT_COUNT, LV.VISIT_RESULT, LV.VISIT_STATE
				ORDER BY ZY.CY_DEP, ZY.CY_DATE, LV.VISIT_STATE";
	
		// TODO 暂时采用本方法获取下一位信息
		$visitSql['where'] = $where;
		$visitSql['fields'] = $fields;
		$visitSql['orderField'] = 'ZY.ZY_CODE';
		$visitSql['orderType'] = 'ASC';
		$_SESSION['visitZySql'] = $visitSql;
		
		$params = $this->request->getGetParameters();
		unset($params['p']);
		unset($params['c']);
		unset($params['a']);

		$totalCount = 0;
		$this->startNum = ($page - 1) * $pageSize + 1;
		$this->rows = JYDB::query($sql, $page, $pageSize, $totalCount);
		
		
		$unAccess = JYDB::query('select count(*) num from ('.$sql.') where visit_state is null');
		$sucAccess = JYDB::query('select count(*) num from ('.$sql.') where visit_state=\'B\'');
		$againAccess = JYDB::query('select count(*) num from ('.$sql.') where visit_state in (\'C\',\'H\',\'G\',\'I\')');
		$sucAccess = $sucAccess[0]['NUM'];
		$againAccess = $againAccess[0]['NUM'];
		$unAccess = $unAccess[0]['NUM'];
		$yetAccess = $totalCount - $unAccess;
		$failAccess = $yetAccess - $sucAccess - $againAccess;
		

		$this->yetAccess = isset($yetAccess)?$yetAccess:0;
		$this->unAccess = isset($unAccess)?$unAccess:0;
		$this->sucAccess = isset($sucAccess)?$sucAccess:0;
		$this->againAccess = isset($againAccess)?$againAccess:0;
		$this->failAccess = isset($failAccess)?$failAccess:0;
		
		$this->pager = JYTools::pager('index.php?c=visitzy&a=index&p=_num_&'.http_build_query($params), $totalCount, $page);
		$this->totalCount = $totalCount;
		$this->startTime = $startTime;
		$this->endTime = $endTime;
		$this->depts = JYDep::getZyDepts(CSM_UNIT_ID);
		$this->selStatus = $selStatus;
		$this->keyword = $keyword;
		
		// 获取当前回访规则对应的问卷id,在生成url时加入url中 ruleID -> ques_id
		$ruleid = ATools::getConfig('app.visitrule.visitzy');
		$ques_id = $this->getQidByRuleId($ruleid['code']);
		$_SESSION['visit']['questnr'] = $ques_id['QID'];
		return $this->render();
	}
	
	// 住院回访个人信息
	public function entryAction() {
		$visitSql = $_SESSION['visitZySql'];
		$fields = $visitSql['fields'];
		$where = $visitSql['where'];
		$patient_id = $this->request->getGetParameter('pid');
		$page = $this->request->getGetParameter('p');
		if($page<1)$page=1;
		$this->next = $page + 1;
		if($patient_id){
			$where =  'P.PATIENT_ID='.$patient_id;
		}
		$sql = "SELECT {$fields} FROM ZHUYUAN ZY 
				LEFT JOIN PATIENT P ON ZY.REF_PATIENT_ID=P.PATIENT_ID
				LEFT JOIN LAST_VISIT LV ON 'ZY_'||ZY.ZY_NO||'_'||ZY.ZY_TIMES=LV.BIZ_CODE
				WHERE {$where}
				GROUP BY P.PATIENT_ID, P.TRUE_NAME, P.PHONE, P.BIRTH, P.SEX, ZY.RY_DATE, ZY.CY_DATE, ZY.CY_DEP, ZY.ZYDOC, ZY_AGE, ZY.BIZ_CODE, ZY.ZY_CODE, ZY.CY_DEP_NO, ZY.ZYDOC_NO,
				LV.LAST_VISIT_TIME, LV.VISIT_COUNT, LV.VISIT_RESULT, LV.VISIT_STATE
				ORDER BY ZY.CY_DEP, ZY.CY_DATE, LV.VISIT_STATE";
		if($patient_id){
			$row = JYDB::query($sql);
		}else{
			$row = JYDB::query($sql, $page, 1);
		}
		
		if (!$row) {
			return $this->alert('对不起，您现在已经回访到最后一位客户了！','/index.php?c=visitzy&a=index');
		}
		
		
		$pid = $row[0]['PATIENT_ID'];
		
		// 获取病人基本资料
		$this->pid = $pid;
		$this->patient = JYDB::find('PATIENT', "PATIENT_ID={$pid}");

		// 获取病人病人的最近住院信息
		$zhuyuan = JYDB::find('ZHUYUAN', "REF_PATIENT_ID={$pid}", '*', 'CY_DATE DESC');
		
		// 病人最近的手术信息
		if ($zhuyuan['IS_OPERATE']) {
			$this->operation = JYDB::findAll('OPERATION', "ZY_NO='{$zhuyuan['ZY_NO']}' AND ZY_TIMES='{$zhuyuan['ZY_TIMES']}'", '*', 'OP_TIMES DESC');
		}
		// 用session来存储上一位患者id
		$_SESSION['page_id'] = $page-1;
		if($patient_id){
			$_SESSION['flag_id'] = $_SESSION['flag_id']>1?($_SESSION['flag_id'] - 1):1;
		}else{
			$_SESSION['flag_id'] = !empty($_SESSION['flag_id'])?($_SESSION['flag_id'] + 1):1;
			if($_SESSION['flag_id']>=count($_SESSION['patient_id'])){
				$_SESSION['patient_id'][$_SESSION['flag_id']] = $row[0]['PATIENT_ID'];
			}
		}
		$chufang = "select r_cf from recipel where r_name like '%".$zhuyuan['RY_ZD']."%' or r_desc  like '%".$zhuyuan['RY_ZD']."%' ";
		$cf = JYDB::query($chufang);
		$this->chufang = $cf;
		$this->zhuyuan = $zhuyuan;
		return $this->render();
	}
	
	function getQidByRuleId($rid){
		return JYDB::find('visit_rule','state=1 and R_ID='.$rid,'ques_id qid');
	}
	
}